package com.manor.main.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;

import com.manor.common.CommonAppContext;
import com.manor.common.activity.AbsActivity;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.StaIntentUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.common.utils.ValidatePhoneUtil;
import com.manor.main.R;

/**
 * 手机号登录
 */
public class OtherPhoneLoginActivity extends AbsActivity implements View.OnClickListener {


    private TextView mTvAgreement;
    private EditText mEtInputPhone;
    private ImageView mIvCloseInput;
    private TextView mBtnMsgcode;
    private CheckBox cbBox;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_other_phone_login;
    }

    @Override
    protected void main() {
        super.main();
        initView();
        initDate();
    }

    private void initDate() {
        mEtInputPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (ValidatePhoneUtil.validateMobileNumber(mEtInputPhone.getText().toString())) {
                    //       mBtnMsgcode.setBackgroundResource(R.drawable.bg_get_codes);
                    mBtnMsgcode.setClickable(true);
                    mBtnMsgcode.setFocusable(true);
                    mIvCloseInput.setVisibility(View.VISIBLE);
                    if (mEtInputPhone.getText().toString().length() == 11) {
                        mBtnMsgcode.setBackgroundResource(R.drawable.bg_get_codes);
                    } else {
                        mBtnMsgcode.setBackgroundResource(R.drawable.bg_get_code);
                    }
                } else {
                    mBtnMsgcode.setBackgroundResource(R.drawable.bg_get_code);
                    mBtnMsgcode.setClickable(false);
                    mBtnMsgcode.setFocusable(false);
                    mIvCloseInput.setVisibility(View.INVISIBLE);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public static void forward() {
        Intent intent = new Intent(CommonAppContext.sInstance, OtherPhoneLoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        CommonAppContext.sInstance.startActivity(intent);
    }

    private void initView() {
        mTvAgreement = findViewById(R.id.tv_agreement);
        mEtInputPhone = findViewById(R.id.et_input_phone);
        mIvCloseInput = findViewById(R.id.iv_close_input);
        mBtnMsgcode = findViewById(R.id.btn_msgcode);
        cbBox = findViewById(R.id.cb_box);

        mIvCloseInput.setOnClickListener(this);
        mBtnMsgcode.setOnClickListener(this);
        mTvAgreement.setOnClickListener(this);
        findViewById(R.id.tv_hide).setOnClickListener(this);
        cbBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_close_input) {
            mEtInputPhone.setText("");
            mIvCloseInput.setVisibility(View.GONE);
            mBtnMsgcode.setBackgroundResource(R.drawable.bg_get_code);
        } else if (id == R.id.btn_msgcode) {
            gotoGenderAgePage();
        } else if (id == R.id.tv_agreement) {
            RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.REGISTRATION_AGREEMENT);
        } else if (id == R.id.tv_hide) {
            RouteUtil.forwardPath(RouteUtil.PC_WEB, StaIntentUtil.PRIVACY_AGREEMENT);
        }
    }

    private void gotoGenderAgePage() {
        if (!cbBox.isChecked()) {
            ToastUtil.show("请勾选协议");
            return;
        }

        String phone = mEtInputPhone.getText().toString();
        if ("".equals(phone)) {
            ToastUtil.show("请输入手机号码");
            return;
        }
        if (!ValidatePhoneUtil.validateMobileNumber(phone)) {
            ToastUtil.show("请输入正确的手机号码");
            return;
        }

        //        CaptchaDialogFragment captchaDialog = (CaptchaDialogFragment) getSupportFragmentManager().findFragmentByTag(CaptchaDialogFragment.class.getSimpleName());
        //        if (captchaDialog == null) {
        //            captchaDialog = new CaptchaDialogFragment();
        //        }
        //        CaptchaDialogFragment finalCaptchaDialog = captchaDialog;
        //        captchaDialog.setOnCaptchaSuccessListener(new CaptchaDialogFragment.OnCaptchaSuccess() {
        //            @Override
        //            public void onCaptchaSuccessListener(CharSequence rid, boolean pass) {
        //                if (pass) {
        //
        //                    finalCaptchaDialog.dismiss();
        //                }
        //            }
        //        });
        //
        //        if (finalCaptchaDialog.isAdded()) {
        //            finalCaptchaDialog.dismiss();
        //        } else {
        //            finalCaptchaDialog.show(getSupportFragmentManager(), CaptchaDialogFragment.class.getSimpleName());
        //        }

        ARouter.getInstance().build(RouteUtil.PC_INPUT_SMSCODE)
                .withString("phone", phone)
                .navigation();


    }
}
